//返回值为需不需要关闭窗口
function yesOperator() {
    var retValue = addData();
    return retValue;
}

function formValid() {
    $("#theform").validate({
        rules: {
            b_samplingCode: {
                required: true,
                isCode:true
            },
            b_samplingName: {
                required: true,
            },
            b_samplingCycle: {
                required: true
            },

            b_rowSort: {
                required: true,
                minlength: 1,
                number: false,
            }

        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent());
        },
        errorElement: "span",
        errorClass: "errortips errortips-w200",
        success: 'valid',
        messages: {
            b_samplingCode: {
                required: " (必填)"
            },
            b_samplingName: {
                required: " (必填)"
            },
            b_samplingCycle:{
                required: " (必填)"
            },
            b_rowSort: {
                required: true,
                minlength: 1,
                isPositiveInteger: true
            }
        },
    });
}



function updateLoad() {
    var params = $$.getQueryString();
    $$.ajax({
        url: "/rest/IotSamplingBusiness/query",
        data: {
            id: params.rowId
        },
        success: function (data) {
            $("#b_id").val(data.id);
            $("#b_samplingCode").val(data.samplingCode);

            $("#b_samplingName").val(data.samplingName);

            $("#b_samplingCycle").val(data.samplingCycle);

            $("#b_status").val(data.status);

            $("#b_rowSort").val(data.rowSort);
            $("#b_remarks").val(data.remarks);

            var status = data.status;
            $("input[name^='b_status_']").each(function () {
                if ($(this).val() == status) {
                    this.checked = 'checked';
                }
            });

            $("#b_samplingCodeOld").val(data.samplingCode);


        }
    });


}

function validForm() {
    return $("#theform").valid();
}

function addData() {
    var param = {};
    var validFormRet;
    $$.form({
        async: false,
        able: function () {
            validFormRet = false;
        },
        checkForm: validForm,
        success: function () {
            validFormRet = true;
        }
    });
    return validFormRet;
}


$(function () {
    formValid();
    $("input[name^='b_status']").click(function () {
        $("#b_status").val($(this).val());
    });
});
jQuery.validator.addMethod("isCode", function(value, element) {

    //校验编码是否存在
    var samplingCodeOld = $("#b_samplingCodeOld").val();
    var samplingCode = $("#b_samplingCode").val();
    if (samplingCodeOld == samplingCode) {
        return true;
    }else{

        var args = {};
        args.c_samplingCode_1 = $("#b_samplingCode").val();
        args.page = 0;
        args.pageSize = 5;
        args.isPage = 1;


        var exists;
        $$.ajax({
            url: "/bootstrap/basegrid/IotSamplingBusiness/list",
            async: false,
            data: args,
            success: function (data) {
                if (data.length && data.length > 0) {
                    exists = true;
                } else {
                    exists = false;
                }
            }
        });
        if(exists){
            $(element).data('error-msg','编码不能重复！');
            return false;
        }
        return true;
    }

}, function(params, element) {
    return $(element).data('error-msg');
});